<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>个人页面</title>
  <script>
    if(/http:/.test(location.href)) location.href = location.href.replace(/http:/, 'https:')
  </script>
  <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jquery"></script>
  <script src="__PUBLIC__/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <script src="__PUBLIC__/node_modules/datatables/media/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" href="__PUBLIC__/node_modules/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="__PUBLIC__/style/manage.css">
  <link rel="stylesheet" href="__PUBLIC__/style/me.css">
  <!-- 弹层 -->
  <script src="__PUBLIC__/script/layer/layer.js"></script>
</head>
<body>
  <!-- 隐藏功能元素区 -->
  <div class="hidden_form">
    <form autocomplete="off" action="{:U('Manageme/manage_me')}" method="get" id = 'hidden_form'></form>
    <input type="hidden" name="转换人员类型" value="{:U('Admin/WebUser/re_web_type')}" id='changeThisTypeURL'>
    <input type="hidden" name="强制按揭员" id="ifAnjie" value="{$Think.session.user.ck_aj}">
  </div>
  <!-- end 隐藏功能元素区 -->

  <div class="container-fluid">
    <div class="col-xs-12" style="height:5px;"></div>
    <div class="col-xs-11 col-xs-offset-1">
      <!-- Nav tabs -->
      <iframe src="{:U('Manageme/me')}?local=tuandui" frameborder="0" style="width:100%;"></iframe>
    </div>

    <div class="main">
      <!-- logo 区域 -->
      <div class="logo_img">
        <if condition="$Think.session.user.login_img neq null">
        <span><img src="__ROOT__/{$Think.session.user.login_img}" alt="LOGO"></span>
        <else/>
        <span><img src="__PUBLIC__/images/dengluye/u5.png" alt="u5"></span>
        </if>
        <br>
        <span>{$Think.session.user.u_name}</span>
      </div>
      <!-- end logo 区域 -->

      <!-- 筛选区 -->
      <div class="search_form col-xs-offset-1 col-xs-11">

        <a class="only_one_type"><input checked style='display:none;' type="radio" name='web_type' id='me_type_1' value='' form='hidden_form'><label for="me_type_1" onclick="setSession2This('')">全部成员</label></a>
        <a class="only_one_type"><input style='display:none;' type="radio" name='web_type' id='me_type_2' value='1' form='hidden_form'><label for="me_type_2" onclick="setSession2This('1')">按揭员</label></a>
        <span class="search_form_container"><span><img src="__PUBLIC__/images/dengluye/u391.png" alt="u391"></span> <input form="hidden_form" name= 'search' type="text" placeholder="姓名、手机号码" class="search_form spec_center"><input form="hidden_form" type="submit" value="搜索" class="search_button"></span>
      </div>
      <!-- end 筛选区 -->

      <!-- 表头 -->
      <div class="col-xs-offset-1 col-xs-10 table_title">
        <span>团队成员</span>
      </div>
      <!-- end 表头 -->

      <!-- 表格区 -->
      <div class="col-xs-offset-1 col-xs-10">
        <table class="messageBoxContent table table-striped table-bordered" id="main_table">
          <thead>
            <tr>
              <th class="No">No.</th>
              <th class="Name">姓名</th>
              <th class="phone">电话</th>
              <th class="date">加盟时间</th>
              <th class="Parent">上级</th>
              <th class="Top">上上级</th>
              <th class="Next">下1级</th>
              <th class="Child">下2级</th>
              <th class="modify">操作</th>
            </tr>
          </thead>
          <tbody>
            <volist name='list' id='vo'>
              <tr>
                <td class="No">{$key+1}</td>
                <td class="Name ye" inori-data='{$vo.web_type}' inori-data-id='{$vo.web_id}'>{$vo.web_name}</td>
                <td class="phone">{$vo.web_phone}</td>
                <td class="date">{$vo.web_time}</td>
                <td class="Parent" title='137888888888'>{$vo.web_id|re_u_n}</td>
                <td class="Top" title = '137888888888'>{$vo.web_id|re_uu_n}</td>
                <td class="Next">{$vo.web_id|re_d}</td>
                <td class="Child">{$vo.web_id|re_dd}</td>
                <td class="modify">
                  <a href="{:U('Uploads/push_zi_user')}?web_id={$vo.web_id}"><img title='下载列表' src="__PUBLIC__/images/dengluye/u693.png" alt="u693"></a>
                  <a href='javascript:void(0);' onclick="frozenThis(this, event, '{$vo.web_id}')" inori-isfrozen='{$vo.is_frozen}'><img title='冻结人员' src="__PUBLIC__/images/neiye/u930.png" alt="u930"></a>
                  <a href="javascript:void(0)" onclick="choseTaken(this, event, '{$vo.web_id}', deleteThisAjax, `{:U('Managezi/get_aj_data_list')}`)"><img title='清理人员' src="__PUBLIC__/images/dengluye/u770.png" alt="u770"></a>
                </td>
              </tr>
            </volist>
          </tbody>
        </table>
        <!-- begin 分页 -->
        <div class="authPage">
            <!-- 页面指示器 -->
            <!-- <div class="pages">24/{$count}</div> -->
            <!-- 分页列表 -->
            <!-- <span class="number">...</span> -->
            <div class="pageJump">
              {$page}
            </div>
          </div>
          <!-- end 分页 -->
      </div>
      <!-- end 表格区 -->

    </div>

    <!-- 页脚 -->
    <div class="col-xs-12">
      <div class="footer text-center"><p class="lead"><strong>深圳市東源匯信息科技有限公司</strong></p></div>
    </div>
    <!-- end 页脚 -->
  </div>

  <script>
    $(document).ready(function () {
      var proList = $('#main_table').DataTable({
        "paging": false,
        "info": false,
        "searching": false
      })
    })
  </script>
  <script>
    $(document).ready(() => {
      $('[inori-data]').each((index, item) => {
        if ($(item).attr('inori-data') === '0') {
          let textValue = `${$(item).html()} <span class='yewu'>业</span>`
          $(item).html(textValue)
        } else if ($(item).attr('inori-data') === '1') {
          let textValue = `${$(item).html()} <span class='an'>按</span>`
          $(item).html(textValue)
        } else {
          console.log(item)
          console.log($(item).attr('inori-data'))
        }
      })
    })
  </script>
  <!-- 转换人员类型 -->
  <script>
    $(document).ready(() => {
      $('span.an').each((index, item) => {
        const ifAnjie = $(`#ifAnjie`).val()
        if (ifAnjie === '0') $(item).css('cursor', 'pointer')
        else $(item).css('display', 'none')
        $(item).one({click: changeThisType}, {_this: this})
      })
      $('span.yewu').each((index, item) => {
        const ifAnjie = $(`#ifAnjie`).val()
        if (ifAnjie === '0') $(item).css('cursor', 'pointer')
        else $(item).css('display', 'none')
        $(item).one({click: changeThisType}, {_this: this})
      })
    })
    // 转换人员类型
    function changeThisType (evt) {
      const _this = evt.target
      const type = $(_this).closest('.Name').attr('inori-data')
      let takenID = ''
      const personID = $(_this).closest('.Name').attr('inori-data-id')
      if (type !== '0' && type !== '1') {
        alert(`未知人员类型：${type}`)
        throw new Error(`未知人员类型：${type}`)
      } else if (type === '1') { // 按揭员降级，弹层
        takenID = choseTaken(_this, evt, personID, changeThisTypeAjax, `{:U('Managezi/get_aj_data_list')}`)
      } else {
        changeThisTypeAjax(_this, evt, personID)
      }

    }
    function changeThisTypeAjax (_this, evt, webID) {
      const type = $(_this).closest('.Name').attr('inori-data')
      const URLBase = $('#changeThisTypeURL').val()
      let AjaxData = {'web_id': webID}
      $.ajax(URLBase, {
        method: 'post',
        data: AjaxData,
        beforeSend: () => {
          $(_this).text('?')
          $(_this).css('cursor', 'not-allowed')
        }
      }).done((data) => {
        if (data.code !== 1) {
          console.log(data.code)
          alert(`${data.message}`)
          layer.closeAll()
          throw new Error(`转换失败：服务器返回：${data.message}`)
          $(_this).one({click: changeThisType}, {_this: this})
        } else {
          const oldType = data.old_type === 1 ? '按揭员' : '业务员'
          const Type = data.old_type === 1 ? '业务员' : '按揭员'
          const TypeClass = data.old_type === 1 ? 'yewu' : 'an'
          const typeText = data.old_type === 1 ? '业' : '按'
          $(_this).attr('class', TypeClass)
          $(_this).text(typeText)
          $(_this).one({click: changeThisType}, {})
          $(_this).css('cursor', 'pointer')
          $(_this).closest('.Name').attr('inori-data', Number(type) === 1 ? 0 : 1)
          alert(`转换成功，用户类型由${oldType} 转换为 ${Type}`)
          layer.closeAll()
        }
      }).fail((err) => {
        $(_this).one({click: changeThisType}, {_this: this})
        layer.closeAll()
        alert('网络连接失败')
        throw new Error('网络连接失败')
      })
    }
    // 冻结人员请求
    function frozenThisAjax (_this, evt, webID) {
      $.ajax(`{:U('Managezi/frozen')}`, {
        method: 'post',
        data: {web_id: webID,is_frozen: $(_this).attr('inori-isfrozen')}
      }).done((data) => {
        layer.closeAll();
        if( String(data.code) === '1') {
          let value = $(_this).attr('inori-isfrozen')
          let textValue = $(_this).find('img').attr('title')
          $(_this).attr('inori-isfrozen', value === 0 ? 1 : 0)
          $(_this).find('img').attr('title', textValue === '冻结人员' ? '解冻人员' : '冻结人员')
          alert(`${textValue}成功`)
        } else {
          alert(`冻结/解冻失败！服务器返回: ${data.message}`)
        }
      }).fail((err) => {
        layer.closeAll();
        alert('网络连接失败')
        throw new Error('网络连接失败')
      })
    }
    // 删除人员请求
    function deleteThisAjax (_this, evt, webID) {
      $.ajax(`{:U('Manageme/del')}`, {
        method: 'post',
        data: {del_arr: `${webID}`}
      }).done((data) => {
        layer.closeAll();
        if( String(data.code) === '1') {
          alert(`删除成功`)
          $(_this).closest('.odd').remove()
        } else {
          alert(`删除失败！服务器返回: ${data.message}`)
        }
      }).fail((err) => {
        layer.closeAll();
        alert('网络连接失败')
        throw new Error('网络连接失败')
      })
    }
    // 冻结人员
    function frozenThis (_this, evt, webID) {
      let textValue = $(_this).find('img').attr('title')
      if (textValue === '冻结人员') choseTaken(_this, evt, webID, frozenThisAjax, `{:U('Managezi/get_aj_data_list')}`)
      else frozenThisAjax(_this, evt, webID)
    }

  </script>
  <!-- 存储页面状态 -->
  <script>
    function setSession2This (type) {
      sessionStorage.setItem('m_m_type', type)
    }
    $(document).ready(() => {
      let value = sessionStorage.getItem('m_m_type')
      if (value === '1') {
        $('#me_type_2').prop('checked', true).change()
      }
    })
  </script>
  <!-- 弹层 -->
  <script>
    // 弹层模板
    const templ = `
      <div style='width:100%;margin:0 auto;'>
        <select name="choseTaker" id="popup_choseTaker" form='not_submit'
        style='margin: 0 auto;width: 15rem;height: 2.5rem;line-height: 2.5rem;display: block;'>
          <option value="false">请等待网络加载</option>
        </select>
      </div>
      <div id='popup_confirm' style='display:none;width:7rem;margin:auto;background:#007cb8;color:#fff;text-align: center;margin-top: 1rem;line-height: 2.5rem;cursor: pointer;'>确认提交</div>
    `.trim()
    function choseTaken(_this, evt, webID, fn, url) {
      let frameID = layer.open({
        type: 1,
        area: ['700px', '450px'],
        title: '选择替换人员',
        content: `${templ}`,
        cancel: (index, layero) => {
          if(confirm('确定要关闭么')){ //只有当点击confirm框的确定时，该层才会关闭
            $(_this).one({click: changeThisType}, {_this: this})
            layer.closeAll()
          }
          return false;
        }
      })
      if(String(typeof frameID).toLowerCase() === 'number' && frameID > 0) {
        let optionList = ``
        $.ajax(`${url}`, {
          method: 'post',
          data: {web_id: webID}
        }).fail((err) => {
          alert(`网络通信失败，请检查网络后重试`)
          // 添加点击
          $(_this).one({click: changeThisType}, {_this: this})
          layer.closeAll()
          throw err
        }).done((res) => {
          if (res.code && String(res.code) !== '1') {
            alert(`网络返回异常：${res.message}`)
            $(_this).one({click: changeThisType}, {_this: this})
            layer.closeAll()
            throw new Error(`网络返回异常：${res.message}`)
          }
          $.each(res.data, (index, item) => {
            optionList += `<option value="${item.web_id}">${item.web_name}</option>`
          })
          $('#popup_choseTaker option').replaceWith(optionList)
          $('#popup_choseTaker').val(null)
          $('#popup_choseTaker').on('change', (evt) => {
            $('#popup_confirm').css('display', 'block')
            $('#popup_confirm').on('click', (evt) => {
              if (fn !== null ) {
                $.ajax(`{:U('Managezi/change_aj_pow')}`, {
                  method: 'post',
                  data: {
                    old_web_id: webID,
                    new_web_id: $('#popup_choseTaker').val()
                  },
                  beforeSend: () => {
                    alert(`正在清洗审核员聊天数据。。。`)
                  }
                }).fail((err) => {
                  debugger
                  alert(`清洗审核员聊天数据发生错误！请在检测网络后刷新页面重试`)
                  // location.reload(true)
                  throw err
                }).done((res) => {
                  debugger
                  fn(_this, evt, webID)
                  $('#popup_confirm').text('正在提交')
                })
              } else {
                // 返回人员id
                // return 1
              }
            })
          })
        })
      } else {
        $(_this).one({click: changeThisType}, {_this: this})
      }
    }
  </script>
</body>
</html>